package com.loan.inceptor;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.loan.service.TokenService;
import com.loan.util.Json;
import com.loan.util.Str;

/**
 * 验签拦截器
 * @author 无处不在的二叔
 */
public class SignInceptor implements Interceptor {
	private TokenService token = new TokenService();
	@Override
	public void intercept(Invocation inv) {
		Controller c = inv.getController();
		String tk = c.getPara("token","");
		Ret ret = token.checkToken(tk);
		if(Str.nb(tk)) {
			if(ret.isOk()){ 
				c.setAttr("user", token.findUserByToken(tk));
				inv.invoke();
			}else{
				c.renderJson(Json.error(ret.getStr("msg")));
			}
		}else{
			c.renderJson(Json.error("TOKEN不可为空！"));
		}
	}
}